package dao

import (
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/coupon"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

//-----优惠券

//优惠券列表
func (d *SiteSettingDao) CouponListDao(baseListReq *request.BaseListReq) *dingo.ErrorWrap {

	var (
		lst    []coupon.Coupon
		count  int64
		limit  = baseListReq.PageSize
		offset = baseListReq.PageSize * (baseListReq.PageNo - 1)
	)
	db := global.Orm.Table("coupon")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

//优惠券编辑
func (d *SiteSettingDao) CouponEditDao(id int, m *coupon.Coupon) *dingo.ErrorWrap {

	db := global.Orm.Table("coupon")
	db.Where("id = ?", id)
	db.Model(&m)
	db.Updates(&m)
	if db.Error != nil {
		return dingo.ErrorIf(db.Error, false)
	}

	return dingo.ErrorIf(db.Error, true)
}

//优惠券保存
func (d *SiteSettingDao) CouponSaveDao(param *coupon.Coupon) *dingo.ErrorWrap {

	db := global.Orm.Table("coupon")
	db.Create(&param)
	return dingo.ErrorIf(db.Error, true)
}

//优惠券删除
func (d *SiteSettingDao) CouponDeleteDao(id int) *dingo.ErrorWrap {
	db := global.Orm.Exec("delete from coupon where id = ?", id)
	return dingo.ErrorIf(db.Error, true)
}
